home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / May / di9805gk / DSMinMax.pas < prev   
Pascal/Delphi Source File  |  1998-01-23  |  10KB  |  397 lines

  1.  
  2. {
  3.   DesignSystems
  4.   Copyright (c) 1996-1998 all rights reserved
  5.  
  6.   Please contact support@dsgnsystms.com with questions or comments
  7. }
  8.  
  9. {$I dsinclud.inc}
  10.  
  11. unit DSMinMax;
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  17.   Forms, Dialogs;
  18.  
  19. type
  20.   TDSMinMax = class(TComponent)
  21.   private
  22.     { Private declarations }
  23.     { We use the following to keep track of which sizes we're controlling
  24.       and what these sizes are... }
  25.     FMaxSizeAssigned      : boolean;
  26.     FMaxSize              : TPoint;
  27.     FMaxPositionAssigned  : boolean;
  28.     FMaxPosition          : Tpoint;
  29.     FMinTrackSizeAssigned : boolean;
  30.     FMinTrackSize         : TPoint;
  31.     FMaxTrackSizeAssigned : boolean;
  32.     FMaxTrackSize         : TPoint;
  33.  
  34.     { These are necessary to handle window subclassing }
  35.     FOldWndProcForm       : TFarProc;
  36.     FOwnerOnShow          : TNotifyEvent;
  37.     FParentForm           : TForm;
  38.     FWindowProcedureForm  : TFarProc;
  39.  
  40.     { A bunch of assessor functions: Get & Set }
  41.     procedure SetMaxSize(const p: TPoint);
  42.     procedure SetMaxPosition(const p: TPoint);
  43.     procedure SetMinTrackSize(const p: TPoint);
  44.     procedure SetMaxTrackSize(const p: TPoint);
  45.  
  46.     function  GetMaxPositionY: integer;
  47.     function  GetMaxPositionX: integer;
  48.     function  GetMaxSizeX: integer;
  49.     function  GetMaxSizeY: integer;
  50.     function  GetMaxTrackSizeX: integer;
  51.     function  GetMaxTrackSizeY: integer;
  52.     function  GetMinTrackSizeX: integer;
  53.     function  GetMinTrackSizeY: integer;
  54.     procedure SetMaxPositionX(const x: integer);
  55.     procedure SetMaxPositionY(const y: integer);
  56.     procedure SetMaxSizeX(const x: integer);
  57.     procedure SetMaxSizeY(const y: integer);
  58.     procedure SetMaxTrackSizeX(const x: Integer);
  59.     procedure SetMaxTrackSizeY(const y: Integer);
  60.     procedure SetMinTrackSizeX(const x: Integer);
  61.     procedure SetMinTrackSizeY(const y: Integer);
  62.  
  63.     { The important functions for subclassing }
  64.     procedure HookForm;
  65.     procedure UnhookForm;
  66.     procedure WndProcForm(var msg: TMessage);
  67.  
  68.   protected
  69.     { Protected declarations }
  70.     procedure OwnerShow(Sender: TObject);
  71.     procedure UpdateParentSize;
  72.  
  73.   public
  74.     { Public declarations }
  75.     constructor Create(AOwner: TComponent);
  76.       override;
  77.     destructor Destroy;
  78.       override;
  79.  
  80.     property MaxSize: TPoint
  81.       read  FMaxSize
  82.       write SetMaxSize;
  83.  
  84.     property MaxPosition: TPoint
  85.       read  FMaxPosition
  86.       write SetMaxPosition;
  87.  
  88.     property MinTrackSize: TPoint
  89.       read  FMinTrackSize
  90.       write SetMinTrackSize;
  91.  
  92.     property MaxTrackSize: TPoint
  93.       read  FMaxTrackSize
  94.       write SetMaxTrackSize;
  95.  
  96.   published
  97.     { Published declarations }
  98.     property MaxHeight: Integer
  99.       read  GetMaxSizeY
  100.       write SetMaxSizeY
  101.       default 0;
  102.  
  103.     property MaxLeft: Integer
  104.       read  GetMaxPositionX
  105.       write SetMaxPositionX
  106.       default 0;
  107.  
  108.     property MaxTop: Integer
  109.       read  GetMaxPositionY
  110.       write SetMaxPositionY
  111.       default 0;
  112.  
  113.     property MaxWidth: Integer
  114.       read  GetMaxSizeX
  115.       write SetMaxSizeX
  116.       default 0;
  117.  
  118.     property ResizeMaxHeight: Integer
  119.       read  GetMaxTrackSizeY
  120.       write SetMaxTrackSizeY
  121.       default 0;
  122.  
  123.     property ResizeMaxWidth: Integer
  124.       read  GetMaxTrackSizeX
  125.       write SetMaxTrackSizeX
  126.       default 0;
  127.  
  128.     property ResizeMinHeight: Integer
  129.       read  GetMinTrackSizeY
  130.       write SetMinTrackSizeY
  131.       default 0;
  132.  
  133.     property ResizeMinWidth: Integer
  134.       read  GetMinTrackSizeX
  135.       write SetMinTrackSizeX
  136.       default 0;
  137.   end;
  138.  
  139. procedure Register;
  140.  
  141. implementation
  142.  
  143. {$IFDEF WIN32}
  144. {$R dsminm32.dcr}
  145. {$ELSE}
  146. {$R dsminm16.dcr}
  147. {$ENDIF}
  148.  
  149. constructor TDSMinMax.Create(AOwner: TComponent);
  150. begin
  151.   inherited Create(AOwner);
  152.  
  153.   if not (csDesigning in ComponentState) then begin
  154.     { If we're running in an application (as opposed to sitting on
  155.       a form in design mode, then take over! }
  156.  
  157.     { Save my parent to make code easier to read }
  158.     FParentForm := (Owner as TForm);
  159.  
  160.     { Catch form show }
  161.     FOwnerOnShow := FParentForm.OnShow;
  162.     FParentForm.OnShow := OwnerShow;
  163.   end;
  164. end;
  165.  
  166. destructor TDSMinMax.Destroy;
  167. begin
  168.   { Stop interfering ... }
  169.   UnhookForm;
  170.  
  171.   inherited Destroy;
  172. end;
  173.  
  174. function  TDSMinMax.GetMaxPositionX: integer;
  175. begin
  176.   Result := FMaxPosition.x;
  177. end;
  178.  
  179. function  TDSMinMax.GetMaxPositionY: integer;
  180. begin
  181.   Result := FMaxPosition.y;
  182. end;
  183.  
  184. function  TDSMinMax.GetMaxSizeX: integer;
  185. begin
  186.   Result := FMaxSize.X;
  187. end;
  188.  
  189. function  TDSMinMax.GetMaxSizeY: integer;
  190. begin
  191.   Result := FMaxSize.Y;
  192. end;
  193.  
  194. function  TDSMinMax.GetMaxTrackSizeX: integer;
  195. begin
  196.   Result := FMaxTrackSize.X;
  197. end;
  198.  
  199. function  TDSMinMax.GetMaxTrackSizeY: integer;
  200. begin
  201.   Result := FMaxTrackSize.Y;
  202. end;
  203.  
  204. function  TDSMinMax.GetMinTrackSizeX: integer;
  205. begin
  206.   Result := FMinTrackSize.X;
  207. end;
  208.  
  209. function  TDSMinMax.GetMinTrackSizeY: integer;
  210. begin
  211.   Result := FMinTrackSize.Y;
  212. end;
  213.  
  214. procedure TDSMinMax.HookForm;
  215. begin
  216.   { Hook the windows procedure of my owner only if I have an owner and
  217.    its window handle has been created }
  218.   if assigned(FParentForm) and FParentForm.HandleAllocated then begin
  219.     { Create the window procedure from one of my methods; be sure to
  220.       pair this with a call to FreeObjectInstance }
  221.     FWindowProcedureForm := MakeObjectInstance(WndProcForm);
  222.  
  223.     { Subclass my form's window by inserting my window procedure into
  224.       the message chain }
  225.     FOldWndProcForm := TFarProc(SetWindowLong(FParentForm.Handle, gwl_WndProc,
  226.       LongInt(FWindowProcedureForm)));
  227.   end;
  228. end;
  229.  
  230. procedure TDSMinMax.OwnerShow(Sender: TObject);
  231. begin
  232.   { My owner is being shown, that means it has a window handle and
  233.     we can subclass it. }
  234.    HookForm;
  235.  
  236.   { Take care of the original show, if any }
  237.   if assigned(FOwnerOnShow) then begin
  238.     FOwnerOnShow(Sender);
  239.     (Owner as TForm).OnShow := FOwnerOnShow;
  240.     FOwnerOnShow := nil;
  241.   end;
  242.  
  243.   { Make sure that the window gets another chance to process wm_GetMinMaxInfo }
  244.   UpdateParentSize;
  245. end;
  246.  
  247. procedure TDSMinMax.SetMaxPositionX(const x: integer);
  248. begin
  249.   SetMaxPosition(Point(x, FMaxPosition.y));
  250. end;
  251.  
  252. procedure TDSMinMax.SetMaxPositionY(const y: integer);
  253. begin
  254.   SetMaxPosition(Point(FMaxPosition.x, y));
  255. end;
  256.  
  257. procedure TDSMinMax.SetMaxSizeX(const x: integer);
  258. begin
  259.   SetMaxSize(Point(x, FMaxSize.y));
  260. end;
  261.  
  262. procedure TDSMinMax.SetMaxSizeY(const y: integer);
  263. begin
  264.   SetMaxSize(Point(FMaxSize.X, y));
  265. end;
  266.  
  267. procedure TDSMinMax.SetMaxTrackSizeX(const x: Integer);
  268. begin
  269.   SetMaxTrackSize(Point(x, FMaxTrackSize.Y));
  270. end;
  271.  
  272. procedure TDSMinMax.SetMaxTrackSizeY(const y: Integer);
  273. begin
  274.   SetMaxTrackSize(Point(FMaxTrackSize.X, y));
  275. end;
  276.  
  277. procedure TDSMinMax.SetMinTrackSizeX(const x: Integer);
  278. begin
  279.   SetMinTrackSize(Point(x, FMinTrackSize.Y));
  280. end;
  281.  
  282. procedure TDSMinMax.SetMinTrackSizeY(const y: Integer);
  283. begin
  284.   SetMinTrackSize(Point(FMinTrackSize.X, y));
  285. end;
  286.  
  287. procedure TDSMinMax.SetMaxPosition(const p: TPoint);
  288. begin
  289.   if not ((p.x = FMaxPosition.x) and (p.y = FMaxPosition.y)) then
  290.     FMaxPositionAssigned := (p.x <> 0) or (p.y <> 0);
  291.  
  292.   FMaxPosition          := p;
  293.   UpdateParentSize;
  294. end;
  295.  
  296. procedure TDSMinMax.SetMaxSize(const p: TPoint);
  297. begin
  298.   if not ((p.x = FMaxSize.x) and (p.y = FMaxSize.y)) then
  299.     FMaxSizeAssigned := (p.x <> 0) or (p.y <> 0);
  300.  
  301.   FMaxSize := p;
  302.   UpdateParentSize;
  303. end;
  304.  
  305. procedure TDSMinMax.SetMaxTrackSize(const p: TPoint);
  306. begin
  307.   if not ((p.x = FMaxTrackSize.x) and (p.y = FMaxTrackSize.y)) then
  308.     FMaxTrackSizeAssigned := (p.x <> 0) or (p.y <> 0);
  309.  
  310.   FMaxTrackSize := p;
  311.   UpdateParentSize;
  312. end;
  313.  
  314. procedure TDSMinMax.SetMinTrackSize(const p: TPoint);
  315. begin
  316.   if not ((p.x = FMinTrackSize.x) and (p.y = FMinTrackSize.y)) then
  317.     FMinTrackSizeAssigned := (p.x <> 0) or (p.y <> 0);
  318.  
  319.   FMinTrackSize := p;
  320.   UpdateParentSize;
  321. end;
  322.  
  323. procedure TDSMinMax.UnhookForm;
  324. begin
  325.   { Undo what Hookform did... reset the window procedure and F